package com.bosch.in.rtp1.ept.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bosch.in.rtp1.ept.constant.FXFactorConstant;
import com.bosch.in.rtp1.ept.dao.FXFactorDao;
import com.bosch.in.rtp1.ept.model.dto.FXFactor;
import com.bosch.in.rtp1.ept.model.technical.Condition4FxFactor;
import com.bosch.in.rtp1.ept.util.SQLStatementLoader;

/**
 * Concrete JDBC implementation of FinishRecipeDao. Provide actions for inserting/updating, searching on FX Factor table.
 * @author LIV1HC
 *
 */

public class FXFactorJDBCDao extends GenericDao implements FXFactorDao{

	/**
	 * Constructor to inject connection
	 * @param con	DB connection
	 */
	
	public FXFactorJDBCDao(Connection connection) {
		super(connection);
	}

	@Override
	public List<FXFactor> getFXFactors(Condition4FxFactor condition4FxFactor) throws SQLException {
		String queryString = SQLStatementLoader.getFXFactorQuery();;
		//excute query
		ResultSet resultSet = executeQuery(queryString, condition4FxFactor.getParameterValues().toArray());
		//convert result set 
		List<FXFactor> fxFactors = new ArrayList<FXFactor>();
		while (resultSet.next()) {
			// save result set to LotStatus object
			FXFactor fxFactor = new FXFactor();
			fxFactor.setYear(resultSet.getInt(FXFactorConstant.YEAR_DB));
			fxFactor.setMonth(resultSet.getInt(FXFactorConstant.MONTH_DB));
			fxFactor.setPriority(resultSet.getInt(FXFactorConstant.PRIORITY_DB));
			fxFactor.setComment(resultSet.getString(FXFactorConstant.COMMENT_DB));
			fxFactor.setProdArea(resultSet.getInt(FXFactorConstant.PRODAREA_DB));			
			fxFactor.setStatus(FXFactorConstant.MANAGED);
			fxFactor.setFxPlan(getFXValueFromResultSet(FXFactorConstant.FXPLAN_DB, resultSet));
			fxFactor.setFxRW(getFXValueFromResultSet(FXFactorConstant.FXRW_DB, resultSet));
			fxFactor.setFxRWH(getFXValueFromResultSet(FXFactorConstant.FXRWH_DB, resultSet));
			fxFactors.add(fxFactor);
		}
		
		// close statement, result set
		resultSet.close();
		close();
		
		return fxFactors;
	}
	
	@Override
	public boolean updateFXFactor(FXFactor fxFactor) throws SQLException {
		Object[] params = new Object[8];
		params[0] = fxFactor.getComment();
		params[1] = getFxValue(fxFactor.getFxRW());
		params[2] = getFxValue(fxFactor.getFxRWH());
		params[3] = getFxValue(fxFactor.getFxPlan());
		params[4] = fxFactor.getYear();
		params[5] = fxFactor.getMonth();
		params[6] = fxFactor.getProdArea();
		params[7] = fxFactor.getPriority();
		
		return executeUpdate(SQLStatementLoader.getFXFactorUpdate(), params);
	}
	
	private Object getFxValue(float value)
	{
		return value == FXFactorConstant.EMPTY_FX_FACTOR ? null : value;
	}
	
	@Override
	public boolean insertFXFactor(FXFactor fxFactor) throws SQLException {
		Object[] params = new Object[8];
		params[0] = fxFactor.getYear();
		params[1] = fxFactor.getMonth();
		params[2] = fxFactor.getProdArea();
		params[3] = fxFactor.getPriority();
		params[4] = fxFactor.getComment();
		params[5] = getFxValue(fxFactor.getFxRW());
		params[6] = getFxValue(fxFactor.getFxRWH());
		params[7] = getFxValue(fxFactor.getFxPlan());
		
		return executeUpdate(SQLStatementLoader.getFXFactorInsert(), params);
	}
	
	/***********PRIVATE METHOD****************/
	
	/**
	 * Get float value of a field in resultSet
	 * @param column: index of the field in resultSet
	 * @param resultSet : ResultSet object 
	 * @return  + FXFactorConstant.EMPTY_FX_FACTOR if value of inputed field is NULL
	 * 			+ float value of that field in other cases  
	 *     
	 */
	private float getFXValueFromResultSet(String column,ResultSet resultSet) throws SQLException
	{
		if (resultSet.getString(column) == null)
		{
			return FXFactorConstant.EMPTY_FX_FACTOR;
		}
		else
		{
			return resultSet.getFloat(column);
		}
	}
	

}
